package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.service.TbItemCatService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class TbItemCatServiceImpl implements TbItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<TbItemCat> listItemCat(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return tbItemCatMapper.selectByExample(example);
    }

    @Override
    public void saveItemCat(Long parentId, String name, Long typeId) {
        TbItemCat tbItemCat = new TbItemCat();
        tbItemCat.setParentId(parentId);
        tbItemCat.setName(name);
        tbItemCat.setTypeId(typeId);
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public TbItemCat selectItemById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateItemCat(Long id, String name, Long typeId) {
        TbItemCat tbItemCat = new TbItemCat();
        tbItemCat.setId(id);
        tbItemCat.setName(name);
        tbItemCat.setTypeId(typeId);
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public void delItems(List<Long> ids) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        ids.stream().forEach(id ->delItemCode(id));//将集合循环出来,给到迭代器方法删除下面的子节点.
        tbItemCatMapper.deleteByExample(example);
    }


    public void delItemCode(Long id){
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        if (tbItemCats== null){
            return;
        }
        tbItemCats.stream().forEach(tbItemCat -> delItemCode(tbItemCat.getId()));
        tbItemCatMapper.deleteByExample(example);

    }
}
